home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / fileutil.zip / MKFIFO.C < prev    next >
C/C++ Source or Header  |  1990-09-02  |  3KB  |  140 lines

  1. /* mkfifo -- make fifo's (named pipes)
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Usage: mkfifo [-m mode] [+mode mode] path...
  19.  
  20.    Options:
  21.    -m, +mode mode    Set the mode of created fifo's to `mode', which is
  22.             symbolic as in chmod and uses the umask as a point of
  23.             departure.
  24.  
  25.    David MacKenzie <djm@ai.mit.edu>  */
  26.  
  27. #include <stdio.h>
  28. #include <getopt.h>
  29. #include <sys/types.h>
  30. #include "system.h"
  31. #include "modechange.h"
  32.  
  33. #ifdef STDC_HEADERS
  34. #include <errno.h>
  35. #include <stdlib.h>
  36. #else
  37. extern int errno;
  38. #endif
  39.  
  40. #if defined(MKFIFO_MISSING)
  41. #define mkfifo(path, mode) (mknod ((path), (mode) | S_IFIFO, 0))
  42. #endif
  43.  
  44. void error ();
  45. void strip_trailing_slashes ();
  46. void usage ();
  47.  
  48. /* The name this program was run with. */
  49. char *program_name;
  50.  
  51. struct option longopts[] =
  52. {
  53.   {"mode", 1, NULL, 'm'},
  54.   {NULL, 0, NULL, 0}
  55. };
  56.  
  57. void
  58. main (argc, argv)
  59.      int argc;
  60.      char **argv;
  61. {
  62.   unsigned short newmode;
  63.   struct mode_change *change;
  64.   char *symbolic_mode;
  65.   int errors = 0;
  66.   int optc;
  67.   int ind;
  68.  
  69.   program_name = argv[0];
  70.   symbolic_mode = NULL;
  71.  
  72. #ifndef S_IFIFO
  73.   error (4, 0, "fifo files not supported");
  74. #else
  75.   while ((optc = getopt_long (argc, argv, "m:", longopts, &ind)) != EOF)
  76.     {
  77.       switch (optc)
  78.     {
  79.     case 0:            /* Long option. */
  80.       break;
  81.     case 'm':
  82.       symbolic_mode = optarg;
  83.       break;
  84.     default:
  85.       usage ();
  86.     }
  87.     }
  88.  
  89.   if (optind == argc)
  90.     usage ();
  91.   
  92.   newmode = 0666 & ~umask (0);
  93.   if (symbolic_mode)
  94.     {
  95.       change = mode_compile (symbolic_mode, 0);
  96.       if (change == MODE_INVALID)
  97.     error (1, 0, "invalid mode");
  98.       else if (change == MODE_MEMORY_EXHAUSTED)
  99.     error (1, 0, "virtual memory exhausted");
  100.       newmode = mode_adjust (newmode, change);
  101.     }
  102.  
  103.   for (; optind < argc; ++optind)
  104.     {
  105.       strip_trailing_slashes (argv[optind]);
  106.       if (mkfifo (argv[optind], newmode))
  107.     {
  108.       error (0, errno, "cannot make fifo `%s'", argv[optind]);
  109.       errors = 1;
  110.     }
  111.     }
  112.  
  113.   exit (errors);
  114. #endif
  115. }
  116.  
  117. #ifdef S_IFIFO
  118. /* Remove trailing slashes from PATH; they cause some system calls to fail. */
  119.  
  120. void
  121. strip_trailing_slashes (path)
  122.      char *path;
  123. {
  124.   int last;
  125.  
  126.   last = strlen (path) - 1;
  127.   while (last > 0 && path[last] == '/')
  128.     path[last--] = '\0';
  129. }
  130.  
  131. void
  132. usage ()
  133. {
  134.   fprintf (stderr, "\
  135. Usage: %s [-m mode] [+mode mode] path...\n",
  136.        program_name);
  137.   exit (1);
  138. }
  139. #endif
  140.